<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class TuanController extends Controller
{
    //进行遍历类别
    static public function getCate($pid){
        //定义一个空数组
        $cate=array();
        //通过传过来的pid查询类别表
        $data=DB::table('cate')->where('pid','=',$pid)->get();
        //遍历查询到的数据
        foreach($data as $k=>$v){
            //通过自身的id去掉用自身的方法(通过pid查询) 判断下面是否还存在子分类 如果有就把他压入自身的一个下标中
            $v['fenlei']=self::getCate($v['cid']);
            //将每次得到的数据遍历到空数组中
            $cate[]=$v;
        }
         return $cate;
    } 

    /*前台团购列表*/
    public function getShow() {
        $cate = self::getCate(0);

        // 获得团购列表数据
        $date = DB::table('tuan')-> join('goods','tuan.gid','=','goods.gid') -> where('tuan.status','=',1) -> where('tuan.end','>',time()) -> select('tuan.tuid','tuan.gid','goods.gname','goods.gpic','goods.gprice','tuan.nprice','tuan.end') -> paginate(20);
        
        // 获得团购上部大图轮播数据
        $date2 = DB::table('tuan')-> join('goods','tuan.gid','=','goods.gid') -> where('tuan.status','=',1) -> where('tuan.end','>',time()) -> select('tuan.tuid','tuan.gid','goods.gname','goods.gpic','goods.gprice','tuan.nprice','tuan.end') -> orderBy('tuan.tuid','desc')-> limit(5) -> get();
       
        return view('home/tuan/show',['cate'=>$cate,'date'=>$date,'date2'=>$date2]);
    }

    /*前台团购详细信息*/
    public function getDetail(Request $request) {
        $cate = self::getCate(0);

        // 获得团购id
        $id = $request -> input('id');

        // 更新浏览量
        DB::table('goods') -> join('tuan','tuan.gid','=','goods.gid') -> where('tuid',$id) -> increment('hot',1);

        // 获得团购id对应的团购详细信息
        $date = DB::table('tuan')-> join('goods','tuan.gid','=','goods.gid') -> where('tuan.tuid',$id) ->  first();
        
        // 获得评论
        $comment = DB::table('comment') -> join('home_user','comment.uid','=','home_user.h_uid') -> where('gid',$id) -> orderBy('id', 'desc') -> paginate(10);

        // 好中差评计算
        $comment2 = DB::table('comment') -> where('gid',$id) -> get();
        $pingjia = ['hao'=>0,'zhong'=>0,'cha'=>0,'zong'=>0];
        foreach($comment2 as $k => $v){
            if($v['eval']==1){
                // 好评数+1 总评数+1
                $pingjia['hao'] += 1;
                $pingjia['zong'] += 1;
            }elseif($v['eval']==2){
                // 中评数+1,总评数+1
                $pingjia['zhong'] += 1;
                $pingjia['zong'] += 1;
            }elseif($v['eval']==3){
                // 差评数+1,总评数+1
                $pingjia['cha'] = 1;
                $pingjia['zong'] += 1;
            }
        }

        // 如果总评价为0,则给总评价个数赋1,可避免计算好评率时出现除0的情况
        if($pingjia['zong']==0){
            $pingjia['zong']=1;
        }

        // 如果有数据,则引用detail模板;如果没有数据,则引用404模板
        if($date){
            return view('home/tuan/detail',['date' => $date,'pingjia'=>$pingjia,'comment' => $comment,'cate'=>$cate]);
        }else{
            return view('errors/404');
        }        
    }

    /*前台团购购买 ,团购不进入购物车,直接进入生成定单的流程*/
    /*确认订单*/
    public function postCc1(Request $request) {
        $cate = self::getCate(0);
        // 判断用户是否登录,如果没有登录跳转登录页
        if(!session('h_user')){
            return redirect('/home/login/index');
        }

        // 接受定单信息
        $req = $request -> except('_token');
        if($req['cnt'] <= 0){
            return back() -> with('error','无法接受0件商品');
        }
               
        // 获得订单的商品信息
        $date = DB::table('tuan')-> join('goods','tuan.gid','=','goods.gid') -> where('tuid',$req['tuid']) -> first();
        $date['cnt'] = $req['cnt'];
        // dd($date);
        $h_uid = session('h_user')['h_uid'];
        // 获得地址内容
        $addr = DB::table('address') -> where('h_uid',$h_uid) -> get();

        // 获得默认地址内容 如果有默认则用默认,如果没有默认用第一条地址
        $defu = DB::table('address') -> where('h_uid',$h_uid) -> where('defu',1) -> first();  
        if(empty($defu)){
            $defu = DB::table('address') -> where('h_uid',$h_uid) -> first();
        }

        return view('/home/tuan/confirm',['date'=>$date,'cate'=>$cate,'addr'=>$addr,'defu'=>$defu]);
    }

    /*生成订单*/
    public function postOo1(Request $request){

        // 获得订单信息
        $aid = $request -> input('aid');
        $date = $request -> except('_token','aid'); 

        // 更新地址表,判断是否是新地址,如果是老地址则进行更新,如果是新地址则添加
        $addrdate = $date;
        unset($addrdate['umsg']);
        unset($addrdate['oprice']);
        unset($addrdate['gid']);
        unset($addrdate['tuid']);
        unset($addrdate['gprice']);
        unset($addrdate['nprice']);
        unset($addrdate['cnt']);
        $addrdate['h_uid'] = session('h_user')['h_uid'];
        if($aid==0){
            DB::table('address') -> insert($addrdate);
        }else{
            DB::table('address') -> where('aid',$aid) -> update($addrdate);
        } 
        
        // 将省市区整合到地址中        
        $date['addr'] = $date['province'].$date['city'].$date['area'].$date['addr']; 

        // 订单数据
        $order['oprice'] = $date['cnt'] * $date['nprice'];
        $order['h_uid'] = session('h_user')['h_uid'];
        $order['ostatus'] = 0;
        $order['otime'] = time();
        $order['rec'] = $date['rec'];
        $order['addr'] = $date['addr'];
        $order['tel'] = $date['tel'];
        $order['umsg'] = $date['umsg'];
        // 更新订单表
        $oid = DB::table('order') -> insertGetId($order);
        
        // 订单详情数据
        $order_detail['oid'] = $oid;
        $order_detail['gid'] = $date['gid'];
        $order_detail['h_uid'] = session('h_user')['h_uid'];
        $order_detail['gprice'] = $date['nprice'];
        $order_detail['gnum'] = $date['cnt'];
        // 更新订单详情表
        DB::table('order_detail') -> insert($order_detail);

        // 更新团购表 更新售出与库存
        DB::table('tuan') -> where('gid',$date['gid']) -> decrement('count',$date['cnt']);
        DB::table('tuan') -> where('gid',$date['gid']) -> increment('sold',$date['cnt']);
        
        // 更新用户表 积分与消费总额
        DB::table('home_user') -> where('h_uid',session('h_user')['h_uid']) -> increment('h_utotal',$order['oprice']);
        DB::table('home_user') -> where('h_uid',session('h_user')['h_uid']) -> increment('h_ujifen',$order['oprice']*7);

        // 返回到订单完成页的信息
        $tuanover['oid'] = $oid;
        $tuanover['oprice'] = $date['cnt'] * $date['nprice'];
        $tuanover['rec'] = $date['rec'];
        $tuanover['addr'] = $date['addr'];
        $tuanover['tel'] = $date['tel'];
        $tuanover['umsg'] = $date['umsg'];
        session(['tuanover'=>$tuanover]);

        return redirect('/home/tuan/oo2');
    }

    /*显示订单成功的页面*/
    public function getOo2(){
        $cate = self::getCate(0);
        return view('/home/tuan/overTuan',['cate'=>$cate]);
    }
}
